package com.mouse.magent.server.handler;

import com.mouse.magent.common.handler.CommonHandler;
import com.mouse.magent.common.protocol.Command;
import com.mouse.magent.common.protocol.Header;
import com.mouse.magent.common.protocol.Message;
import io.netty.channel.ChannelHandlerContext;

/**
 * 代理远程请求
 */
public class RemoteProxyHandler extends CommonHandler {
    private ChannelHandlerContext proxyCtx;

    public RemoteProxyHandler(ChannelHandlerContext proxyCtx) {
        this.proxyCtx = proxyCtx;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Message message = new Message();
        message.setType(Command.REMOTE_CONNECT);
        message.setHeader(new Header(ctx.channel().id().asLongText()));
        proxyCtx.writeAndFlush(message);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Message message = new Message();
        message.setType(Command.REMOTE_DISCONNECT);
        message.setHeader(new Header(ctx.channel().id().asLongText()));
        proxyCtx.writeAndFlush(message);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Message message = new Message();
        message.setType(Command.REMOTE_DATA);
        message.setData((byte[]) msg);
        message.setHeader(new Header(ctx.channel().id().asLongText()));
        proxyCtx.writeAndFlush(message);
    }
}
